RBScript Class

Used to execute REALbasic code within a running (compiled) application.

Events

CompilerError

Input

Print

RuntimeError


Properties

Context

EncodingFont

Source

State


Methods

PartialRun

Precompile

Reset

Run


More information available in parent classes: Object


Notes

The RBScript language is an implementation of the REALbasic language that allows end users to write and execute REALbasic code within a compiled application. Scripts are compiled into machine language, rather then being interpreted.

Since RBScript is a class, you use it by creating an instance of this class either via code or by adding an RBScript control to a window. The easiest way to use RBScript is to assign the code to the Source property of the RBScript object and call the Run method.

To provide information to an RBScript while it's running, use the Input function. This function calls the Input event of the RBScript object where you can return the information you wish returned by the Input function in your RBScript code. In the following example, the results of the Input function are assigned to a variable:

Dim Years, Days as Integer
Years = Val(Input(""))
Days = Years * 365

The Input function takes a String that can be used to provide a prompt in case you are going to provide a dialog box in which the user enters the information. Since the Input function returns a String and we want to store the value as an integer, the Val function is used to convert the string to an integer. In this case, if the number of years is going to be entered into an EditField called Editfield1, then the Input event of the RBScript object would look like this:

When the Run method of the RBScript object is called, the code will be compiled and then executed. Since the Input function is called, the Input event of the RBScript object is executed and the contents of the Text property of the EditField is returned and assigned to the Years variable. Then the Days value is calculated.

Getting Information From RBScript

You obtain data from the RBScript using the Print method. This method takes a String and passes it to the Print event of the RBScript object. Here is the example modified to use the Print function:

Dim Years, Days as Integer
Years = Val(Input(""))
Days = Years * 365
Print Str(days)

You access the value passed to the Print method through the Print event. For example, if you want to assign the value to the Text property of a StaticText object, the code for the Print event of the RBScript object would look like this:

Sub Print(msg as String)
 StaticText1.text = msg

Handling Errors in Your Code

If an error occurs while RBScript is compiling your code, the CompilerError event of the RBScript object will be called and will be passed appropriate error information so you can then decide how to handle the error. If the error occurs while the code is running, the RuntimeError event of the RBScript object is called and is passed appropriate error information. You can then decide how to respond to the error.

Variables and Constants

All numeric and alphanumeric operators are supported:

+, -, *, /, \, Mod, <, =, >, <=, >=, <>.

Logical operators: And, Not, Or.

All forms of comments are supported: ', //, and REM.

Data Types

RBScript supports the following data types:

Integer

Single

Double

Boolean

String

Color

Object

Variant

Arrays can use any of these types.

Control Structures

All the control structures specified in this Language Reference are supported. This includes:

Function...

Sub...

For... Next

Do... Loop

If... Then... End If

Select Case... End Select

While...Wend

Return statement

Classes

You can create a class using the Class...End Class structure. A new class can have properties, methods, and events.

Class NewClass
  Dim i as Integer //Property definitions..
  Dim c as Color
  Sub myMethod (x as Integer, y as Integer)
  //Method definition goes here
 End Sub
 //Class definition
End Class

A class can be subclassed from another class using the "Inherits" declaration.

Modules

You can create modules using the Module structure. For example:

Module Foo
  Dim bar As Integer
  Sub Baz ()
  bar = 42
 End Sub
End Module

Modules are similar to REALbasic modules. The differences are that properties are always protected and constants are not allowed. You can get the effect of a public property or constant by simply putting the Dim or Const statement outside of the module.

TypeCasting

Typecasting is supported. Use the desired type's name as a function whose only argument is the object reference you want to cast. If it fails, it will trigger an IllegalCastException error.

"Super" keyword

The new "Super" keyword lets you call overridden methods without having to specify which class the method came from. This works for constructors as well. You can call overridden superclass methods using REALbasic's classname.methodname() syntax.

Inheritance

Class inheritance is implemented with the "Inherits" declaration.

Class MyNewSubClass
 Inherits NewClass
 //continue coding here....
End Class

Events

Classes can declare new events using a New Event declaration.

Class myClass
  New Event myEvent (myParameter as DataType) as DataType
End Class

Methods can handle such events using the Handles Event declaration. For example:

Class myClass
  New Event myEvent (myParameter as DataType) as DataType
End Class
Class mySubClass
 Inherits myClass
 Function myEvent (myParameter as DataType) Handles Event
 //continue with function here
 End Function
End Class

Interfaces

RBScript now supports interfaces, declared by the Interface...End Interface structure. Interface methods are declared with a Sub or Function line just as they would be declared inside a class, but they have no contents and need no End Sub or End Function line:

Interface MovableItem
  Sub Move( x As Integer, y As Integer)
  Function LocationX() As Integer
  Function LocationY() As Integer
End Interface

Function declarations with no parameters must have empty parentheses, as shown above.

A class may declare that it implements an interface with the Implements keyword:

Class Box
 Implements MovableItem
End Class

Classes can implement any number of interfaces, but they must provide all of the methods listed in the interface. Interfaces can be used in all the situations as in REALbasic.

Input and Output

FunctionComments
Input(Prompt as String) Retrieves input from the user. This function triggers the Input event.
Print(String) Sends output to the implied output device. This function triggers the Print event.

Functions

Standard library functions are supported as follows. These functions work as specified in this Language Reference unless comments indicate otherwise. String functions can be called using the syntax of methods, just as in REALbasic.

FunctionComment
Abs( Double) As Double
Acos( Double) As Double
Asc( String) As Integer
AscB( String) As Integer
Asin( Double) As Double
Atan( Double) As Double
Atan2( Double, Double) As Double
BitwiseAnd( Integer, Integer) As Integer
BitwiseOr( Integer, Integer) As Integer
BitwiseXor( Integer, Integer) As Integer
ByRef keyword
ByVal keyword
CDbl( String) As Double Identical to Val().
Ceil( Double) As Double
Chr( Double) As String
ChrB( Double) As String
CMY
Color
Const name = value
Cos( Double) As Double
CountFields( String, String) As Integer
CStr( Double) as String
Dim Allows you to declare multiple variables with different types rather than only multiple variables of the same type. Empty arrays can be created by specifying -1 elements.
Do...Loop
Exit
Exp( Double) As Double
False
Floor( Double) As Double
For...Next Allows floating point loop counters and step values.
Format( Double, String) As String
Function Functions can be called before they are defined.
Goto keyword
Hex( Integer) As String
HSV
If...Then...Else
InStr( Integer, String, String) As Integer
InStrB( Integer, String, String) As Integer
IsA operator Can be used with Object; it will return True for any non-null object.
Left( String, Integer) As String
LeftB( String, Integer) As String
Len( String) As Integer
LenB( String) As Integer
Log( Double) As Double
Lowercase( String) As String
LTrim( String) As String
Max( Double, Double) As Double
Me
Microseconds As Double
Mid( String, Integer, Integer) As String
MidB( String, Integer, Integer) As String
Min( Double, Double) As Double
Nil
NthField( String, String, Integer) As String
Oct( Integer) As String
Pow( Double, Double) As Double
Redim
Rem
Replace( String, String, String) As String
ReplaceB( String, String, String) As String
ReplaceAll( String, String, String) As String
ReplaceAllB( String, String, String) As String
RGB
Right( String, Integer) As String
RightB( String, Integer) As String
Rnd As Double
Round( Double) As Double
RTrim( String) As String
Select Case
Self
Sin( Double) As Double
Sqrt( Double) As Double
Str( Double) As String
StrComp( String, String, Integer) As Integer
Sub
Tan( Double) As Double
Ticks as Integer
Titlecase( String) As String
Trim( String) As String
True
Ubound(array) As Integer Supports second parameter for multi-dimensional arrays.
Uppercase( String) As String
Val( String) As Double Scientific format not recognized.
While...Wend

Compiler error numbers returned in errorNumber are shown below:

Error NumberDescription
1 Syntax does not make sense.
2 Type mismatch.
3 Select Case does not support that type of expression.
4 The compiler is not implemented (obsolete).
5 The parser's internal stack has overflowed.
6 Too many parameters for this function.
7 Not enough parameters for this function call.
8 Wrong number of parameters for this function call.
9 Parameters are incompatible with this function.
10 Assignment of an incompatible data type.
11 Undefined identifier.
12 Undefined operator.
13 Logic operations require Boolean operands.
14 Array bounds must be integers.
15 Can't call a non-function.
16 Can't get an element from something that isn't an array.
17 Not enough subscripts for this array's dimensions.
18 Too many subscripts for this array's dimensions.
19 Can't assign an entire array.
20 Can't use an entire array in an expression.
21 Can't pass an expression as a ByRef parameter.
22 Duplicate identifier.
23 The backend code generator failed.
24 Ambiguous call to overloaded method.
25 Multiple inheritance is not allowed.
26 Cannot create an instance of an interface.
27 Cannot implement a class as though it were an interface.
28 Cannot inherit from something that is not a class.
29 This class does not fully implement the specified interface.
30 Event handlers cannot live outside of a class.
31 It is not legal to ignore the result of a function call.
32 Can't use "Self" keyword outside of a class.
33 Can't use "Me" keyword outside of a class.
34 Can't return a value from a Sub.
35 An exception object required here.
36-39 Obsolete.
40 Destructors can't have parameters.
41 Can't use "Super" keyword outside of a class.
42 Can't use "Super" keyword in a class that has no parent.